/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lidroid.xutils_sample.slice;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils_sample.ResourceTable;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.io.File;

public class HttpUtilsPageSlice extends AbilitySlice {

    static final HiLogLabel lable = new HiLogLabel(HiLog.LOG_APP, 0x00201, "HttpUtilsPageSlice");

    private String msgString = "users:";

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_http_utils_page);
        Text btnDown1 = (Text) findComponentById(ResourceTable.Id_btn_download1);
        Text down1Txtview = (Text) findComponentById(ResourceTable.Id_download1_textView);
        btnDown1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                HttpUtils http = new HttpUtils();
                http.send(HttpRequest.HttpMethod.GET,
                        "https://www.baidu.com/",
                        new RequestCallBack<String>() {
                            @Override
                            public void onLoading(long total, long current) {
                                down1Txtview.setText(current + "/" + total);
                            }
                            @Override
                            public void onSuccess(String result) {
                                down1Txtview.setText(result);
                            }
                            @Override
                            public void onStart() {
                            }
                            @Override
                            public void onFailure(Throwable error, String msg) {
                                HiLog.error(lable, msgString + error.toString());
                                HiLog.error(lable, msgString + msg);
                                down1Txtview.setText(msg);
                            }
                        });
            }
        });

        Text btnDown2 = (Text) findComponentById(ResourceTable.Id_btn_download2);
        Text down2Txtview = (Text) findComponentById(ResourceTable.Id_download2_textView);
        btnDown2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                HttpUtils http = new HttpUtils();
                File file = HttpUtilsPageSlice.this.getExternalFilesDir("test");
                String filePath = file.getPath() + "QQ9.4.6.27770.exe";
                HiLog.debug(lable, msgString + filePath);
                HttpHandler handler = http.download("https://download.pc6.com/down/866479/",
                        filePath,
                        new RequestCallBack<File>() {

                            @Override
                            public void onStart() {
                                down2Txtview.setText("conn...");
                            }

                            @Override
                            public void onLoading(long total, long current) {
                                down2Txtview.setText(current + "/" + total);
                            }

                            @Override
                            public void onSuccess(File result) {
                                down2Txtview.setText("downloaded:" + result.getPath());
                            }


                            @Override
                            public void onFailure(Throwable error, String msg) {
                                HiLog.error(lable, msgString + "onFailure");
                                down2Txtview.setText(msg);
                            }
                        });
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
